import { ref } from 'vue';
const columns = ref(
  [
    {
      title: $t('table.FlowName'),
      name: $t('table.FlowName'),
      dataIndex: 'flowName',
      key: 'flowName',
      minWidth: 200,
      maxWidth: 500,
      sorter: true,
      ellipsis: true,
      resizable: true,
      fixed: 'left',
      align: 'center'
    },
    {
      title: $t('table.FlowStatus'),
      dataIndex: 'flowStatus',
      key: 'flowStatus',
      sorter: true,
      minWidth: 60,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    {
      title: '采集任务',
      dataIndex: 'flowName1',
      key: 'flowName1',
      sorter: true,
      minWidth: 60,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    {
      title: '源库类型',
      key: 'dbType',
      minWidth: 80,
      dataIndex: 'dbType',
      sorter: true,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    {
      title: '目标库类型',
      minWidth: 80,
      key: 'reserve2',
      dataIndex: 'reserve2',
      sorter: true,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    {
      title: '任务组',
      minWidth: 80,
      key: 'groupName',
      dataIndex: 'groupName',
      sorter: true,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    // {
    //   title: $t('table.StatusDetails'),
    //   minWidth: 80,
    //   key: 'objTypeCount',
    //   dataIndex: 'objTypeCount',
    //   sorter: true,
    //   ellipsis: true,
    //   resizable: true,
    //   align: 'center',
    // },
    // {
    //   title: $t('table.SourceDbAliasName'),
    //   key: 'objCount',
    //   minWidth: 120,
    //   dataIndex: 'objCount',
    //   sorter: true,
    //   ellipsis: true,
    //   resizable: true,
    //   align: 'center',
    // },
    // {
    //   title: $t('table.TargetDbAliasName'),
    //   minWidth: 120,
    //   key: 'objSize',
    //   dataIndex: 'objSize',
    //   sorter: true,
    //   ellipsis: true,
    //   resizable: true,
    //   align: 'center',
    // },
    // {
    //   title: $t('table.TotalDelayTime'),
    //   key: 'groupName',
    //   dataIndex: 'groupName',
    //   sorter: true,
    //   ellipsis: true,
    //   minWidth: 120,
    //   align: 'center',
    //   resizable: true,
      
    // },
    {
      title: $t('table.Createtime'),
      key: 'createTime',
      dataIndex: 'createTime',
      sorter: true,
      minWidth: 200,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    {
      title: $t('table.BackFlowMode'),
      key: 'userName',
      dataIndex: 'userName',
      sorter: true,
      minWidth: 140,
      ellipsis: true,
      resizable: true,
      align: 'center',
    },
    {
      title: $t('table.Action'),
      key: 'operateList',
      fixed: 'right',
      width: 110,
      align: 'center',
    },
  ]);

// 从sessionStorage获取模块配置并提取xoneBusiness模块路径
const getXoneBusinessPath = () => {
  try {
    const moduleKey = sessionStorage.getItem('moduleKey');
    if (!moduleKey) return '';

    const modules = JSON.parse(moduleKey);
    const xoneModule = modules.find(module => module.module === 'xoneBusiness');
    return xoneModule?.modulePath || '';
  } catch (error) {
    console.error('获取xoneBusiness路径失败:', error);
    return '';
  }
};

const xoneBusiness = getXoneBusinessPath();
//计算目标端出现次数
/**
 * 计算数组中每个元素出现的次数
 * @param {Array} array - 需要统计的数组
 * @returns {Object} 返回一个对象，key为数组元素，value为该元素出现的次数
 */
const countDuplicates = (array) => {
  if (!Array.isArray(array)) {
    console.warn('countDuplicates: 输入参数必须是数组');
    return {};
  }

  return array.reduce((countMap, item) => {
    if (item != null) { // 排除null和undefined
      countMap[item] = (countMap[item] || 0) + 1;
    }
    return countMap;
  }, {});
}
// 计算任务进度
/**
 * 计算同步任务的完成进度百分比
 * @param {string} syncCount - 格式为"已完成数/总数"的字符串，如"5/10"
 * @returns {number} 返回0-100之间的进度百分比
 */
const computedCount = (syncCount) => {
  if (!syncCount || typeof syncCount !== 'string') {
    return 0;
  }

  try {
    const [completed, total] = syncCount.split('/').map(Number);

    if (isNaN(completed) || isNaN(total) || total === 0) {
      return 0;
    }

    return Math.min(100, Math.max(0, parseFloat(((completed / total) * 100).toFixed(1))));
  } catch (error) {
    console.error('计算进度出错:', error);
    return 0;
  }
};
// 表格目标端数据源个数
/**
 * 获取目标端数据类型
 * @param {string} type - 目标端数据类型
 * @returns {string} 返回目标端数据类型，如果type为空则返回空字符串
 */
const getDtType = (type) => type || '';
// 任务延迟 转换时间
const formatSeconds = (value) => {
  var theTime = parseInt(value);// 秒
  var theTime1 = 0;// 分
  var theTime2 = 0;// 小时
  // alert(theTime);
  if (theTime > 60) {
    theTime1 = parseInt(theTime / 60);
    theTime = parseInt(theTime % 60);
    // alert(theTime1+"-"+theTime);
    if (theTime1 > 60) {
      theTime2 = parseInt(theTime1 / 60);
      theTime1 = parseInt(theTime1 % 60);
    }
  }
  var result = "" + parseInt(theTime) + $t('tableIndex.Second');
  if (theTime1 > 0) {
    result = "" + parseInt(theTime1) + $t('tableIndex.Minute') + result;
  }
  if (theTime2 > 0) {
    result = "" + parseInt(theTime2) + $t('tableIndex.Hour') + result;
  }
  return result;
}
const getTotalDelayTime = (t) => {
  var time = formatSeconds(t)
  let color = '#0685cc'
  time.indexOf('分') != -1 ? color = '#e6ab52' : 0
  time.indexOf('小时') != -1 ? color = '#951d11' : 0
  return { time, color }
}
// 表格--源端--提示显示
const tSrcPop = ref([
  {
    title: $t('tableIndex.Datasourcetype'),
    dataIndex: 'type',
    key: 'type',
    align: 'center',
  },
  {
    title: $t('tableIndex.Datasourcenum'),
    dataIndex: 'num',
    key: 'num',
    align: 'center',
  },
])
// 表格--源端--提示显示
const tSrcPopTa = (a) => {
  let ob = countDuplicates(a)
  let arr = []
  for (var i in ob) {
    arr.push({
      type: i,
      num: ob[i],
    })
  }
  return arr
}
// 表格--展示项显示
const srcShow = ref([
  {
    title: $t('tableIndex.Datasourcename'),
    key: 'sourceDbAliasName',
    align: 'center',
  },
  {
    title: 'Ip:',
    key: 'sourceDbIp',
    align: 'center',
  },
  {
    title: $t('tableIndex.Port'),
    key: 'sourceDbPort',
    align: 'center',
  },
  {
    title: $t('tableIndex.Instancename'),
    key: 'sourceDbName',
    align: 'center',
  },
  {
    title: $t('tableIndex.Username'),
    key: 'sourceDbUser',
    align: 'center',
  },
])
const tarShow = ref([
  {
    title: $t('tableIndex.Datasourcename'),
    key: 'targetDbAliasName',
    align: 'center',
  },
  {
    title: 'Ip:',
    key: 'targetDbIp',
    align: 'center',
  },
  {
    title: $t('tableIndex.Port'),
    key: 'targetDbPort',
    align: 'center',
  },
  {
    title: $t('tableIndex.Instancename'),
    key: 'targetDbName',
    align: 'center',
  },
  {
    title: $t('tableIndex.Username'),
    key: 'targetDbUser',
    align: 'center',
  },
])
const getUserGid = () => window.sessionStorage.getItem('userGid') || 1;
export {
  columns,
  xoneBusiness,
  countDuplicates,
  computedCount,
  getDtType,
  formatSeconds,
  getTotalDelayTime,
  tSrcPop,
  tSrcPopTa,
  srcShow,
  tarShow,
  getUserGid
}
